package Communication;

import java.awt.Image;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import Communication.*;
import Entity.Message;
import Entity.Pattern;
import Entity.Customer;
import Entity.Report;
import Entity.Category;
import Entity.Product;
import Entity.Worker;



public class Client extends AbstractClient  {
	Worker currentWorker;
	ArrayList<Customer> customerList;
	ArrayList<Pattern> patternList;
	 ArrayList<Report> reportList;
	
	ArrayList<Product> productList;
	ArrayList<Worker> workerList;
	ArrayList<Category> categoryList;
	
	final public static int DEFAULT_PORT = 5555;

	
	public Client(String host, int port) throws IOException {
		super(host, port);
		openConnection();
		System.out.println("Client  connected successfuly");
		
		currentWorker = new Worker();
		customerList = new ArrayList<Customer>();
		patternList = new ArrayList<Pattern>();
		// ArrayList<Report> reportList;
		productList = new ArrayList<Product>();
		categoryList = new ArrayList<Category>();
		workerList = new ArrayList<Worker>();
		
	}
	
	
	public void handleMessageFromServer(Message msg) {

		String Msg =  msg.getMsg();
		Object data;
		
	
		

		switch (Msg) 
		{
		  case "worker":
			data=(Worker)msg.getObj();
			setCurrentWorker((Worker)msg.getObj());
			break;
		}}
			//*/
		/*case CSMessage.WORKER:
			data = (User) Original.getData();
			setCurrentUser((User) data);
			break;
		
		
			
		case MessageHandle.MULTIPLE_PHOTOS:
			int i;
			
			ArrayList<Photo> photos =(ArrayList<Photo>)Original.getData();
			for(i=0;i<photos.size();i++)
			{
				Image image; 
				byte[] array = photos.get(i).getByteArray();
				 InputStream in = new ByteArrayInputStream(array);
			    try {
			        image = ImageIO.read(in);
			        photos.get(i).setPhoto(image);
			        image.flush();/////////////////////////////////
			       } catch (IOException e) {
			        e.printStackTrace();
			       }
			}
			
	        this.photoList = photos; 
			break;		
			
		case MessageHandle.MULTIPLE_COMPLAINTS:
		case MessageHandle.SINGLE_COMPLAINT:
			//complaintList.clear();
			complaintList = (ArrayList<Complaint>)Original.getData();
			break;
			
		case MessageHandle.MULTIPLE_CUSTOMERS:
			//customerList.clear();
			customerList = (ArrayList<Customer>)Original.getData();
			break;
			
		case MessageHandle.PACKAGE_REQUEST:
			 //packRequestList.clear();
			 packRequestList = (ArrayList<PackageRequest>) Original.getData();
			break;
		
		case MessageHandle.PHOTO_PACK:
			ArrayList<PhotoPackage> bla = (ArrayList<PhotoPackage>) Original.getData();
			packageList.addAll(bla);
			break;
		
		case MessageHandle.LAYER_PACK:
			ArrayList<LayerPackage> blabla = (ArrayList<LayerPackage>) Original.getData();
			packageList.addAll(blabla);
			break;			
		}*/
	
		

	
	
	
		
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	public void setCurrentWorker(Worker worker)
	{
		currentWorker =worker; 
	}

	
	
	public Worker getCurrentWorker() {
		return currentWorker;
	}

	
	public void quit() {
		try {
			closeConnection();
		} catch (IOException e) {
		}
		System.exit(0);
	}

	
	
	public void getData(Message message) {

		try {
			sendToServer(message);
		} catch (IOException e) {
			e.printStackTrace();
			quit();
		}
	}


	/**
	 * @param photoList
	 */
	public void setProductlist(ArrayList<Product> productList) {
		this.productList = productList	;	
	}

	/**
	 * @return
	 */
	public ArrayList<Product> getProductList() {
		return productList;
	}

	/**
	 * @return complaintList
	 */
	public ArrayList<Pattern> getPatternList() {
		return patternList;
	}
	/*public void setPatternList(ArrayList<Pattern> pattern){
		patternList = pattern;
	}*/
	
	/**
	 * @return customerList
	 */
	public ArrayList<Customer> getCustomerList() {
		return customerList;
	}
	
	/**
	 * @return packageList
	 */
	public ArrayList<Category> getCategoryList() {
		return categoryList;
	}
	
	/**
	 * @return packRequestList
	 */
	public ArrayList<Report> getReportList() {
		return reportList;
	}
	
	/**
	 * @return workerList
	 */
	public ArrayList<Worker> getWorkerList() {
		return workerList;
	}


	@Override
	protected void handleMessageFromServer(Object msg) {
		// TODO Auto-generated method stub
		
	}

	
}

